package com.example.online.music.controller;

import com.example.online.music.common.R;
import com.example.online.music.service.AppLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/appLogin")
public class AppLoginController {

    @Autowired
    private AppLoginService appLoginService;

    /**
     * 返回支付宝登录的扫码页面Uri
     * @return
     */
    @GetMapping("/alipay/authUri")
    public R alipayLogin(){
        return appLoginService.getAlipayAuthUri();
    }

    /**
     * 通过auth_code换取accessToken和openId
     * @param authCode
     * @param response
     * @return
     */
    @RequestMapping("/callback")
    public R callback(@RequestParam("auth_code") String authCode,
                      HttpServletResponse response) {
        R r = appLoginService.getAccessTokenAndUserId(authCode);
//        response.setHeader("refresh", "1;url=http://127.0.0.1:8080");
        return r;
    }
}
